﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Common.DBUtility
{
    public static class PagerHelper
    {
        #region 方法扩展
        /// <summary>    
        /// IEnumerable extensions    
        /// </summary>    
        public static PageList<T> ToPageList<T>(this IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
        {
            return new PageList<T>(source, pageIndex, pageSize, totalCount);
        }
        /// <summary>
        /// string extensions
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static bool IsMatch(this string s, string pattern)
        {
            if (s == null) return false;
            else return Regex.IsMatch(s, pattern);
        }
        /// <summary>
        /// string extensions
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static string Match(this string s, string pattern)
        {
            if (s == null) return string.Empty;
            return Regex.Match(s, pattern).Value;
        }
        /// <summary>
        /// string extensions
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static bool IsEmail(this string s)
        {
            if (string.IsNullOrWhiteSpace(s)) return false;
            else return Regex.IsMatch(s, @"^[a-z0-9-_.]+@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z0-9]{1,18}([\.][a-z0-9]{1,9})*$", RegexOptions.IgnoreCase);
        }
        /// <summary>
        /// string extensions
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static bool IsPhone(this string s)
        {
            if (s == null) return false;
            else return Regex.IsMatch(s, @"^\d{11}|(\d{3,4}-)?\d{6,8}$", RegexOptions.IgnoreCase);
        }
        public static bool IsMobile(this string s)
        {
            if (s == null) return false;
            else return Regex.IsMatch(s, @"^\d{11}$", RegexOptions.IgnoreCase);
        }
        #endregion
    }
}